def bin_variance(x,y,bins=20):
    def variance(lst):
        avg=sum(lst)*1.0/len(lst)
        squared=reduce(lambda a,b: a+b**2,lst,0)*1.0/len(lst)
        return squared - avg*avg
    
    d_x=list(enumerate(x))
    d_x.sort(key=lambda v:v[1])
    step=len(d_x)/bins
    out=[]
    for i in xrange(0,len(d_x),step):
        input_bin=d_x[i:i+step]
        output_bin=map(lambda v:y[v[0]],input_bin)
        out.append({'xmin':i,'xmax':i+step,'variance':variance(output_bin)})
    return out

def main():
    x=xrange(10,20)
    y=xrange(100,200,10)
    z=xrange(10,20)
    w=[0]*10

    oy=bin_variance(x,y,bins=5)
    oz=bin_variance(x,z,bins=5)
    ow=bin_variance(x,w,bins=5)
    print (oy)
    print (oz)
    print (ow)

main()
